﻿Imports System.Data.SqlClient

Public Class DataProvider
    Public Shared strCon = "Database=QLTV;Server=(local)\SQLEXPRESS;Integrated Security=SSPI;"

    Public Shared Function ConnectToDB() As SqlConnection
        Dim con As New SqlConnection(strCon) '
        Try
            con.Open()
        Catch ex As Exception
            Throw New ArgumentOutOfRangeException("Chuỗi kết nối bị lỗi!")
        End Try
        Return con
    End Function

    Public Shared Function loadDataTable(ByVal sql As String) As DataTable
        Dim dt As New DataTable
        Dim con = ConnectToDB()
        Dim com As New SqlCommand(sql, con)

        Dim da As New SqlDataAdapter(com)
        da.Fill(dt)

        con.Close()
        Return dt
    End Function

    Public Shared Sub ExecuteNonQuery(ByVal sql As String)
        Dim con = ConnectToDB()
        Dim com As New SqlCommand(sql, con)
        Try
            com.ExecuteNonQuery()
        Catch ex As Exception
            Throw New ArgumentException("Không thể thực hiện được thao tác")
        Finally
            con.Close()
        End Try
    End Sub

    Public Shared Function loadDataWithStore(ByVal query As String) As DataTable
        Dim con As SqlConnection = ConnectToDB()
        Dim dt As New DataTable
        Dim cmd As New SqlCommand(query, con)
        cmd.CommandType = CommandType.StoredProcedure
        Dim da As New SqlDataAdapter(cmd)
        da.Fill(dt)
        con.Close()
        Return dt

    End Function


End Class

